//https://code.jquery.com/jquery-3.1.0.min.js
var system = require('system');
var webpage = require('webpage');
var fs = require('fs');

var url = system.args[1];

var img_path = system.args[2];

// create and set page
var page = webpage.create();

//console.log(url);

page.onConsoleMessage = function (msg) {
    //console.log('console: ' + msg);
};

page.viewportSize = {
    width:  1024,
    height: 768 * 3
}

//我的Initialized
page.onInitialized = function() {
    page.evaluate(function () {
        window.Audio =  function() {
            return {
                play: function() {},
                pause: function() {},
                canPlayType: function() {},
                accessKey: function(){},
                accessibleNode: function(){},
                assignedSlot: function(){},
                attributes: function(){},
                audioTracks: function(){},
                autoplay: function(){},
                baseURI: function(){},
                buffered: function(){},
                childElementCount: function(){},
                childNodes: function(){},
                children: function(){},
                classList: function(){},
                className: function(){},
                clientHeight: function(){},
                clientLeft: function(){},
                clientTop: function(){},
                clientWidth: function(){},
                computedName: function(){},
                computedRole: function(){},
                contentEditable: function(){},
                controls: function(){},
                controlsList: function(){},
                crossOrigin: function(){},
                currentSrc: function(){},
                currentTime: function(){},
                dataset: function(){},
                defaultMuted: function(){},
                defaultPlaybackRate: function(){},
                dir: function(){},
                disableRemotePlayback: function(){},
                draggable: function(){},
                duration: function(){},
                ended: function(){},
                error: function(){},
                firstChild: function(){},
                firstElementChild: function(){},
                hidden: function(){},
                id: function(){},
                inert: function(){},
                innerHTML: function(){},
                innerText: function(){},
                inputMode: function(){},
                isConnected: function(){},
                isContentEditable: function(){},
                lang: function(){},
                lastChild: function(){},
                lastElementChild: function(){},
                localName: function(){},
                loop: function(){},
                mediaKeys: function(){},
                muted: function(){},
                namespaceURI: function(){},
                networkState: function(){},
                nextElementSibling: function(){},
                nextSibling: function(){},
                nodeName: function(){},
                nodeType: function(){},
                nodeValue: function(){},
                nonce: function(){},
                offsetHeight: function(){},
                offsetLeft: function(){},
                offsetParent: function(){},
                offsetTop: function(){},
                offsetWidth: function(){},
                onabort: function(){},
                onauxclick: function(){},
                onbeforecopy: function(){},
                onbeforecut: function(){},
                onbeforepaste: function(){},
                onblur: function(){},
                oncancel: function(){},
                oncanplay: function(){},
                oncanplaythrough: function(){},
                onchange: function(){},
                onclick: function(){},
                onclose: function(){},
                oncontextmenu: function(){},
                oncopy: function(){},
                oncuechange: function(){},
                oncut: function(){},
                ondblclick: function(){},
                ondrag: function(){},
                ondragend: function(){},
                ondragenter: function(){},
                ondragleave: function(){},
                ondragover: function(){},
                ondragstart: function(){},
                ondrop: function(){},
                ondurationchange: function(){},
                onemptied: function(){},
                onencrypted: function(){},
                onended: function(){},
                onerror: function(){},
                onfocus: function(){},
                onfullscreenchange: function(){},
                onfullscreenerror: function(){},
                ongotpointercapture: function(){},
                oninput: function(){},
                oninvalid: function(){},
                onkeydown: function(){},
                onkeypress: function(){},
                onkeyup: function(){},
                onload: function(){},
                onloadeddata: function(){},
                onloadedmetadata: function(){},
                onloadstart: function(){},
                onlostpointercapture: function(){},
                onmousedown: function(){},
                onmouseenter: function(){},
                onmouseleave: function(){},
                onmousemove: function(){},
                onmouseout: function(){},
                onmouseover: function(){},
                onmouseup: function(){},
                onmousewheel: function(){},
                onpaste: function(){},
                onpause: function(){},
                onplay: function(){},
                onplaying: function(){},
                onpointercancel: function(){},
                onpointerdown: function(){},
                onpointerenter: function(){},
                onpointerleave: function(){},
                onpointermove: function(){},
                onpointerout: function(){},
                onpointerover: function(){},
                onpointerup: function(){},
                onprogress: function(){},
                onratechange: function(){},
                onreset: function(){},
                onresize: function(){},
                onscroll: function(){},
                onsearch: function(){},
                onseeked: function(){},
                onseeking: function(){},
                onselect: function(){},
                onselectstart: function(){},
                onstalled: function(){},
                onsubmit: function(){},
                onsuspend: function(){},
                ontimeupdate: function(){},
                ontoggle: function(){},
                ontouchcancel: function(){},
                ontouchend: function(){},
                ontouchmove: function(){},
                ontouchstart: function(){},
                onvolumechange: function(){},
                onwaiting: function(){},
                onwaitingforkey: function(){},
                onwebkitfullscreenchange: function(){},
                onwebkitfullscreenerror: function(){},
                onwheel: function(){},
                outerHTML: function(){},
                outerText: function(){},
                ownerDocument: function(){},
                parentElement: function(){},
                parentNode: function(){},
                paused: function(){},
                playbackRate: function(){},
                played: function(){},
                prefix: function(){},
                preload: function(){},
                previousElementSibling: function(){},
                previousSibling: function(){},
                readyState: function(){},
                remote: function(){},
                scrollHeight: function(){},
                scrollLeft: function(){},
                scrollTop: function(){},
                scrollWidth: function(){},
                seekable: function(){},
                seeking: function(){},
                shadowRoot: function(){},
                sinkId: function(){},
                slot: function(){},
                spellcheck: function(){},
                src: function(){},
                srcObject: function(){},
                style: function(){},
                styleMap: function(){},
                tabIndex: function(){},
                tagName: function(){},
                textContent: function(){},
                textTracks: function(){},
                title: function(){},
                translate: function(){},
                videoTracks: function(){},
                volume: function(){}

            };
        };
    });
};


//我的onLoadFinished
page.onLoadFinished = function(status) {

}

page.onResourceRequested = function(requestDate, networkRequest) {
    var res = requestDate.url;
    if(/google.*.com/i.test(res)) {
         networkRequest.abort();
    }
}


page.onResourceReceived = function(response) {
    // console.log(response.url);
}

page.onResourceTimeout = function(response) {

}

page.onResourceError = function(resourceError) {
    // phantom.exit();
    // body...
}

// send request
page.open(url,  function(status) {
//    console.log('start...');
	setTimeout(checkReadyState, 3000);
});

function checkReadyState() {
//    console.log('coming!')
    var readyState = page.evaluate(function () {
            return document.readyState;
        });
    // console.log(readyState);
    if ("complete" === readyState) {
        pageContext = onPageReady();
        console.log(pageContext);
        page.render(img_path);
        setTimeout(function() {
            phantom.exit();
        }, 10000);
    }
}

setTimeout(function() {
    phantom.exit();
}, 40000);

function onPageReady() {
    return page.evaluate(function () {
        return document.documentElement.innerHTML;
    });
}